package com.itheima.security.distributed.order.common;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.security.distributed.order.model.UserDto;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public class PreAuthFun {

    public PreAuthFun() {
    }

    public boolean hasRole(String role) {
        return this.hasAnyRole(role);
    }

    public boolean hasAnyRole(String... role) {
        // 获取用户信息
        UserDto user = JwtUtil.getUser();
        if (user == null) {
            return false;
        } else {
            String userRole = user.getRoleName();
            if (StringUtils.isBlank(userRole)) {
                return false;
            } else {
                List<String> roles = Convert.toList(String.class, userRole);
                for(int var = 0; var < role.length; ++var) {
                    String r = role[var];
                    if (CollectionUtil.contains(roles, r)) {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
